package com.service.infoEntry.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.config.utils.*;
import com.service.infoEntry.dao.*;
import com.service.infoEntry.dto.*;
import com.service.infoEntry.entity.*;
import com.service.infoEntry.service.TeamService;
import com.service.infoEntry.service.TransactionService;
import com.service.infoEntry.service.UserInfoService;
import com.service.infoEntry.vo.ProgrammeVo;
import com.service.infoEntry.vo.TeamVo;
import com.service.infoEntry.vo.TransactionVo;
import com.service.infoEntry.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.service.config.utils.DateUtils.YYYY_MM_DD_HH_MM_SS;
import static com.service.infoEntry.constant.Constant.*;

/**
 * @author hjl
 * @date 2021/10/22
 */
@Slf4j
@Service
public class TransactionServiceImpl implements TransactionService {
    @Resource
    private TransactionInfoDao transactionInfoDao;
    @Resource
    private UserInfoService userService;
    @Resource
    private TeamService teamService;
    @Resource
    private ProgrammeServiceImpl programmeService;
    @Resource
    private ProgrammeDao programmeDao;
    @Resource
    private TeamDao teamDao;
    @Resource
    private ContentOfDeleteDao deleteDao;
    @Resource
    private ContentOfUpdateDao updateDao;

    @Value("${spring.fileUrl}")
    String fileUrl;
    @Value("${spring.filePath}")
    String filePath;
    private final Integer PAGE_SIZE = 10;
    private Integer NO = 1;
    private final String transactionModelFile="transactionModelFile.xlsx";

    @Override
    public Result addTransaction(TransactionInfoDto transactionInfoDto,UserDto userDto) {
        TeamVo teamVo = teamService.queryTeamByTeamId(transactionInfoDto.getTeamId());
        if(null == teamVo)
        {
            return Result.failure(TEAM_NOT_EXIST,"当前无团队");
        }
        TransactionInfo transactionInfo = ModelMapperUtil.strictMap(transactionInfoDto, TransactionInfo.class);
        //判断是否绑定了项目
        if(null!=transactionInfoDto.getProgrammeId() && !"".equals(transactionInfoDto.getProgrammeId()))
        {
            ProgrammeVo programmeVo = programmeService.queryProgrammeByNo(transactionInfoDto.getProgrammeId());
            if(null!=programmeVo)
            {
                transactionInfo.setProgrammeName(programmeVo.getName());
            }
        }

        transactionInfo.setId(IdUtil.getStringId());
        transactionInfo.setTransactionTime(DateUtils.getNowDate());
        transactionInfo.setConclusion(0);
        transactionInfo.setUpdateTime(DateUtils.getNowDate());

        //生成编号(如:20200101001-001)
        String transactionNO = this.generateTransactionNO();
        List<String> allTransactionNos = transactionInfoDao.queryAllNo(transactionInfoDto.getTeamId());

        //避免问题编号重复
        if(allTransactionNos!=null && allTransactionNos.size()>0)
        {
            boolean flag = true;
            while(flag)
            {
                for (String no : allTransactionNos) {
                    if(transactionNO.equals(no))
                    {
                        transactionNO = this.generateTransactionNO();
                        flag = true;
                        break;
                    }
                    flag = false;
                }
            }
        }
        transactionInfo.setNo(transactionNO);

        //往事务进展记录表中插入数据
        if(null != transactionInfo.getProcess() && !"".equals(transactionInfo.getProcess()))
        {
            this.persistentProcess(transactionInfo,userDto);
        }
        transactionInfoDao.saveTransaction(transactionInfo);
        return Result.ok();
    }

    /**
     * 自动生成项目编号 20200101001
     */
    private String generateTransactionNO()
    {
        //20200101010101
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String today =  simpleDateFormat.format(date);

        //20200101001
        String no = "T"+today+StringUtils.leftPad(String.valueOf(NO++), 3, '0');
        return no;
    }

    @Override
    public Result updateTransaction(TransactionInfoDto transactionInfoDto,UserDto userDto) {
        if(null != transactionInfoDto)
        {
            TransactionInfo transactionInfo = ModelMapperUtil.strictMap(transactionInfoDto, TransactionInfo.class);
            transactionInfo.setUpdateTime(DateUtils.getNowDate());

            ProgrammeVo programmeVo = programmeService.queryProgrammeByNo(transactionInfoDto.getProgrammeId());
            if(null != programmeVo)
            {
                transactionInfo.setProgrammeName(programmeVo.getName());
            }

            //往事务进展记录表中插入数据
            if(null != transactionInfo.getProcess() && !"".equals(transactionInfo.getProcess()))
            {
//                StringBuilder process=new StringBuilder(transactionInfo.getProcess());
//                process.append(" ").append(userDto.getUsername()).append(" ").append(DateUtils.parseDateToStr("yyyy-dd-MM hh:mm",new Date()));
                this.persistentProcess(transactionInfo,userDto);
//                transactionInfo.setProcess(new String(process));
            }
            transactionInfoDao.updateTransaction(transactionInfo);
            return Result.ok();
        }
        return Result.failure(DATA_IS_WRONG,DATA_ERROR);
    }

    @Override
    public void closeTransaction(TransactionInfoDto transactionInfoDto) {
        if(null != transactionInfoDto)
        {
            TransactionInfo transactionInfo = ModelMapperUtil.strictMap(transactionInfoDto, TransactionInfo.class);
            transactionInfo.setUpdateTime(DateUtils.getNowDate());
            transactionInfo.setStatus(4);
            transactionInfoDao.updateTransaction(transactionInfo);
        }
    }

    @Override
    public List<TransactionInfo> queryAllByDate(TransactionInfo transactionInfo) {
        return transactionInfoDao.queryAllByDate();
    }

    @Override
    public void downloadModelTransaction(HttpServletResponse response) {
        programmeService.downLoadFile(response,filePath,transactionModelFile);
    }

    @Override
    public TransactionVo searchTransactionByNo(String transactionNo,String userId,String teamId) {
        TransactionVo transactionVo=new TransactionVo();
        TransactionInfo transactionInfo=transactionInfoDao.searchTransactionByNo(transactionNo);
        boolean flag=false;
        if(null==transactionInfo)
        {
            //从已删除数据表中查询
            List<ContentOfDeleteInfo> contentOfDeleteInfos = deleteDao.queryDeletedContent(teamId, userId, "0", "");
            for (ContentOfDeleteInfo contentOfDeleteInfo : contentOfDeleteInfos) {
                try {
                    TransactionInfo transactionInfo1 = JsonXMLUtils.json2obj(contentOfDeleteInfo.getContent(), TransactionInfo.class);
                    if(transactionInfo1.getNo().equals(transactionNo))
                    {
                        transactionInfo=transactionInfo1;
                        flag=true;
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if(null != transactionInfo)
        {
            transactionVo = ModelMapperUtil.strictMap(transactionInfo, TransactionVo.class);
            transactionVo.setDeleted(flag);
            TeamVo teamVo = teamService.queryTeamByTeamId(transactionInfo.getTeamId());
            UserVo userVo = userService.queryByUserId(transactionInfo.getUserId());
            if(userVo.getId().equals(userId))
            {
                userVo.setCurrentUser(true);
            }
            if(teamVo!=null)
            {
                transactionVo.setTeamName(teamVo.getTeamName());
                if (userVo.getFullName() == null) {
                    userVo.setFullName("");
                }
                transactionVo.setUserVo(userVo);
            }
            return transactionVo;
        }
        return null;
    }

    @Override
    public Map<String,Object> searchTransaction(String teamId, String userId, String desc, Integer status, Integer transactionStatus, Integer page) {
        Map<String,Object> map = new HashMap<>(16);
        List<TransactionVo> transactionVoList = new ArrayList<>();
        TransactionVo transactionVo1;
        UserVo userVo1;
        if(desc!=null)
        {
            desc="%"+desc+"%";
        }
        PageHelper.startPage(page,PAGE_SIZE,"transaction_time DESC");
        List<TransactionInfo> transactionInfos = transactionInfoDao.queryOpenTransactionOfTeam(teamId,userId,desc,status,transactionStatus);
        PageInfo<TransactionInfo> pageInfo = new PageInfo(transactionInfos);
        if(null!=transactionInfos && transactionInfos.size()>0)
        {
            for (TransactionInfo transactionInfo : transactionInfos) {
                transactionVo1 = ModelMapperUtil.strictMap(transactionInfo,TransactionVo.class);
                userVo1 = userService.queryByUserId(transactionInfo.getUserId());
                if(transactionInfo.getUserId().equals(userId))
                {
                    userVo1.setCurrentUser(true);
                }
                if (userVo1.getFullName() == null) {
                    userVo1.setFullName("");
                }
                transactionVo1.setUserVo(userVo1);

                TeamVo teamVo = teamService.queryTeamByTeamId(transactionInfo.getTeamId());
                transactionVo1.setTeamName(teamVo.getTeamName());

                transactionVoList.add(transactionVo1);
            }
        }
        transactionVoList=transactionVoList.stream().sorted(Comparator.comparing(TransactionVo::getTransactionTime).reversed()).collect(Collectors.toList());

        map.put("transactions",transactionVoList);
        map.put("pageSize",PAGE_SIZE);
        map.put("pageIndex",page);
        map.put("totalRecords",pageInfo.getTotal());
        map.put("totalPages",pageInfo.getPages());
        return map;
    }

    @Override
    public String exportTransactionList(String teamId,String userId) {
        //声明所有导出问题集合
        List<ExportTransactionInfoDto> exportTransactionList = new ArrayList<>();
        //当前用户的当前团队下的所有公开项目和自己的所有项目
        List<TransactionInfo> transactionInfoList = transactionInfoDao.queryOpenTransactionOfTeam(teamId,userId,null,null,1);
        if(null==transactionInfoList || transactionInfoList.size()<=0)
        {
            return null;
        }

        //当前团队下所有用户
        List<UserInfo> userInfos = teamDao.queryAllUserInTeam(teamId);
        //当前团队信息
        TeamInfo teamInfo = teamDao.queryTeamByTeamId(teamId);

        List<ContentOfUpdateInfo> processContentList = updateDao.queryUpdateContent(teamId, userId, "0", "", "");
        //排序
        processContentList=processContentList.stream().sorted(Comparator.comparing(ContentOfUpdateInfo::getUpdateTime).reversed()).collect(Collectors.toList());

        ExportTransactionInfoDto exportTransaction;
        List<String> processList;
        String processStr="";
        StringBuilder process = new StringBuilder();
        for (TransactionInfo transactionInfo : transactionInfoList) {
            exportTransaction = ModelMapperUtil.strictMap(transactionInfo, ExportTransactionInfoDto.class);

            for (UserInfo info : userInfos) {
                if(transactionInfo.getUserId().equals(info.getId()))
                {
                    if (info.getFullName() == null) {
                        exportTransaction.setUser(info.getUsername()+"");
                    }else {
                        exportTransaction.setUser(info.getUsername()+info.getFullName());
                    }

                }
            }
            exportTransaction.setTeamName(teamInfo.getTeamName());

            processList=new ArrayList<>();
            for (ContentOfUpdateInfo updateInfo : processContentList) {
                if(updateInfo.getContentNo().equals(transactionInfo.getNo()))
                {
                    if (null==updateInfo.getFullName() || "".equals(updateInfo.getFullName()))
                    {
                        processStr = DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,updateInfo.getUpdateTime()) + " " + updateInfo.getUsername()+" "+updateInfo.getContent() ;
                    }else {
                        processStr = DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,updateInfo.getUpdateTime()) + " " + updateInfo.getUsername()+ " " +updateInfo.getFullName()+" "+updateInfo.getContent() ;
                    }
                    processList.add(processStr);
                }
            }
            for (String s : processList) {
                process=process.append(s+"\n");
            }
            exportTransaction.setProcess(new String(process));

            //设置问题创建时间和定位时长
            exportTransaction.setCreateTime(DateUtils.parseDateToStr("yyyy-MM-dd hh:mm:ss",transactionInfo.getTransactionTime()));
            String datePoor="";
            if(exportTransaction.getStatus()==4)
            {
                datePoor = DateUtils.getDatePoor(transactionInfo.getUpdateTime(), transactionInfo.getTransactionTime());
                exportTransaction.setPositionTime(datePoor.substring(0,datePoor.indexOf("天")+1));
            }else {
                datePoor = DateUtils.getDatePoor(DateUtils.getNowDate(),transactionInfo.getTransactionTime() );
                exportTransaction.setPositionTime(datePoor.substring(0,datePoor.indexOf("天")+1));
            }


            exportTransactionList.add(exportTransaction);
        }
        EasyExcel.write(filePath+"事务清单_"+exportTransactionList.get(0).getNo()+".xlsx",ExportTransactionInfoDto.class).sheet().doWrite(exportTransactionList);
        return fileUrl+"事务清单_"+exportTransactionList.get(0).getNo()+".xlsx";
    }

    @Override
    public Result uploadTransactionList(MultipartFile file, String teamId,UserDto userDto) throws Exception {
        TransactionInfo transactionInfo;
        ProgrammeInfo programmeInfo;
        // 1.判断文件是否为空
        if (file == null) {
            return Result.failure(UPLOAD_EXCEL_EMPTY, UPLOAD_EXCEL_EMPTY_INFO);
        }
        // 2.判断上传内容是否符合要求
        String fileName = file.getOriginalFilename();
        // 判断是否是excel文件
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            return Result.failure(UPLOAD_EXCEL_ERROR, UPLOAD_EXCEL_ERROR_INFO);
        }
        //获取Excel表格数据
        List<UploadTransactionDto> uploadTransactionDtoList = programmeService.batchImport(file,UploadTransactionDto.class,2);
        if (null==uploadTransactionDtoList)
        {
            return Result.failure(FILE_ISNULL,"当前文件无数据");
        }
        //校验Excel数据
        List<ProgrammeInfo> programmeInfos = programmeDao.queryAllProgrammeInTeam(teamId);
        Map<Integer, String> result = checkTransaction(uploadTransactionDtoList);
        //数据有异常,返回错误信息
        if(result.containsKey(0))
        {
            return Result.failure(DATA_FORMAT_WRONG,result.get(0));
        }
        for (UploadTransactionDto uploadTransactionDto : uploadTransactionDtoList) {
            if(null!=uploadTransactionDto)
            {
                transactionInfo=new TransactionInfo();
                transactionInfo=ModelMapperUtil.strictMap(uploadTransactionDto,TransactionInfo.class);
                transactionInfo.setIsUrgency(parseIsUrgency(uploadTransactionDto.getIsUrgencyStr()));
                transactionInfo.setId(IdUtil.getStringId());
                transactionInfo.setNo(generateTransactionNO());
                //判断是否关联项目
                if (uploadTransactionDto.getProgrammeName()!=null && !"".equals(uploadTransactionDto.getProgrammeName()))
                {
                    programmeInfo = programmeInfos.stream().filter(p -> p.getName().equals(uploadTransactionDto.getProgrammeName())).findFirst().get();
                    transactionInfo.setProgrammeName(programmeInfo.getName());
                    transactionInfo.setProgrammeId(programmeInfo.getId());
                }
                transactionInfo.setTransactionTime(DateUtils.getNowDate());
                transactionInfo.setTeamId(teamId);
                transactionInfo.setUpdateTime(DateUtils.getNowDate());
                transactionInfo.setUserId(userDto.getId());
                transactionInfo.setStatus(parseTransactionStatus1(uploadTransactionDto.getStatusStr()));
                transactionInfo.setTransactionStatus(parseTransactionStatus2(uploadTransactionDto.getTransactionStatusStr()));
                transactionInfo.setConclusion(parseTransactionConclusion(uploadTransactionDto.getConclusionStr()));

                //往事务进展记录表中插入数据
                if(null != transactionInfo.getProcess() && !"".equals(transactionInfo.getProcess()))
                {
                    this.persistentProcess(transactionInfo,userDto);
                }
                transactionInfoDao.saveTransaction(transactionInfo);
            }
        }
        return Result.ok();
    }

    @Override
    public List<ProgrammeInfo> getProgrammeName(String teamId, String userId,int type) {
        List<ProgrammeInfo> programmeInfos = programmeDao.queryProgramme(teamId,userId,null,null,1);
        programmeInfos.stream().sorted(Comparator.comparing(ProgrammeInfo::getAddTime).reversed()).collect(Collectors.toList());
        if (type==1)
        {
            return programmeInfos;
        }else if(type==0)
        {
            List<ProgrammeInfo> deletedProgrammes=new ArrayList<>();
            List<ContentOfDeleteInfo> contentOfDeleteInfos = deleteDao.queryDeletedContent(teamId, userId, "1", "");
            contentOfDeleteInfos.stream().forEach(contentOfDeleteInfo -> {
                try {
                    ProgrammeInfo programmeInfo = JsonXMLUtils.json2obj(contentOfDeleteInfo.getContent(), ProgrammeInfo.class);
                    deletedProgrammes.add(programmeInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            programmeInfos.addAll(deletedProgrammes);
            return programmeInfos;
        }
        return null;
    }

    @Override
    public List<ContentOfUpdateInfo> queryUpdateProcess(String teamId, String userId,String transactionNo) {
        List<ContentOfUpdateInfo> contentOfDeleteInfos = updateDao.queryUpdateContent(teamId, userId, "0", "",transactionNo);
        contentOfDeleteInfos=contentOfDeleteInfos.stream().sorted(Comparator.comparing(ContentOfUpdateInfo::getUpdateTime).reversed()).collect(Collectors.toList());
        return contentOfDeleteInfos;
    }

    /**
     * 持久化事务进展
     */
    public void persistentProcess(TransactionInfo transactionInfo,UserDto userDto)
    {
        if (null != transactionInfo.getProcess() && !"".equals(transactionInfo.getProcess()))
        {
            ContentOfUpdateInfo updateInfo=new ContentOfUpdateInfo();

            updateInfo.setId(IdUtil.getStringId());
            updateInfo.setTeamId(transactionInfo.getTeamId());
            updateInfo.setContent(new String(transactionInfo.getProcess()));
            updateInfo.setContentNo(transactionInfo.getNo());
            updateInfo.setContentName(transactionInfo.getDesc());
            updateInfo.setStatus(transactionInfo.getStatus().toString());
            updateInfo.setContentType("0");
            updateInfo.setUserId(userDto.getId());
            updateInfo.setUsername(userDto.getUsername());
            updateInfo.setFullName(userDto.getFullName());
            updateInfo.setUpdateTime(DateUtils.getNowDate());
            updateDao.addUpdateContent(updateInfo);
        }
    }

    /**
     * 校验上传Excel的数据
     * @param uploadTransactionDtoList
     * @return
     */
    public Map<Integer,String> checkTransaction(List<UploadTransactionDto> uploadTransactionDtoList)
    {
        Map<Integer,String> result=new HashMap<>();
        for (int i=0;i<uploadTransactionDtoList.size();i++)
        {
            if(null!=uploadTransactionDtoList.get(i))
            {
                //所属项目名称
//                String programmeName = uploadTransactionDtoList.get(i).getProgrammeName();

/*                if(null==programmeName || "".equals(programmeName) || programmeInfos.stream().noneMatch(p->p.getName().equals(programmeName)))
                {
                    result.put(0,"第"+(i+1)+"条事务的项目名称不在当前团队");
                    return result;
                }*/

                if(null==uploadTransactionDtoList.get(i).getTransactionStatusStr() || "".equals(uploadTransactionDtoList.get(i).getTransactionStatusStr()))
                {
                    result.put(0,"第"+(i+1)+"条事务是否公开状态为空");
                    return result;
                }else if(null==parseTransactionStatus2(uploadTransactionDtoList.get(i).getTransactionStatusStr()))
                {
                    result.put(0,"第"+(i+1)+"条事务是否公开状态填写不合法");
                    return result;
                }

                if(null==uploadTransactionDtoList.get(i).getStatusStr() || "".equals(uploadTransactionDtoList.get(i).getStatusStr()))
                {

                    result.put(0,"第"+(i+1)+"条事务的状态填写为空");
                    return result;
                }else if(null==parseTransactionStatus1(uploadTransactionDtoList.get(i).getStatusStr()))
                {
                    result.put(0,"第"+(i+1)+"条事务的状态填写不合法");
                    return result;
                }

                if (null==uploadTransactionDtoList.get(i).getDesc()||"".equals(uploadTransactionDtoList.get(i).getDesc()))
                {
                    result.put(0,"第"+(i+1)+"个项目的事务/问题描述输入不能为空");
                    return result;
                }
                if (null==uploadTransactionDtoList.get(i).getProgrammeDesc()||"".equals(uploadTransactionDtoList.get(i).getProgrammeDesc()))
                {
                    result.put(0,"第"+(i+1)+"个项目的事务/问题项目说明输入不能为空");
                    return result;
                }
                if (null==uploadTransactionDtoList.get(i).getContact()||"".equals(uploadTransactionDtoList.get(i).getContact()))
                {
                    result.put(0,"第"+(i+1)+"个项目的事务/问题联系人输入不能为空");
                    return result;
                }
                if (null==uploadTransactionDtoList.get(i).getOrigin()||"".equals(uploadTransactionDtoList.get(i).getOrigin()))
                {
                    result.put(0,"第"+(i+1)+"个事务/问题的事务来源输入不能为空");
                    return result;
                }
                if (null==uploadTransactionDtoList.get(i).getIsUrgencyStr()||"".equals(uploadTransactionDtoList.get(i).getIsUrgencyStr()))
                {
                    result.put(0,"第"+(i+1)+"个事务/问题的是否紧急字段输入不能为空");
                    return result;
                }
                if (null==uploadTransactionDtoList.get(i).getClientName()||"".equals(uploadTransactionDtoList.get(i).getClientName()))
                {
                    result.put(0,"第"+(i+1)+"个事务/问题的客户名称输入不能为空");
                    return result;
                }
                if (null==uploadTransactionDtoList.get(i).getType()||"".equals(uploadTransactionDtoList.get(i).getType()))
                {
                    result.put(0,"第"+(i+1)+"个事务/问题的问题分类输入不能为空");
                    return result;
                }
                if (null==uploadTransactionDtoList.get(i).getServiceInvolved()||"".equals(uploadTransactionDtoList.get(i).getServiceInvolved()))
                {
                    result.put(0,"第"+(i+1)+"个事务/问题的涉及的华为乾坤云服务输入不能为空");
                    return result;
                }
            }
        }
        result.put(1,"");
        return result;
    }

    /**
     * 解析事务状态:0-启动;1-下单;2-交付;3-运维;4-关闭
     * @param status
     * @return
     */
    public Integer parseTransactionStatus1(String status)
    {
        switch (status)
        {
            case "发现":
                return 0;
            case "定位":
                return 1;
            case "现网带缺陷闭环":
                return 2;
            case "挂起":
                return 3;
            case "正式闭环":
                return 4;
            default:
                return null;
        }
    }

    /**
     * 解析事务状态:true-1-公开;false-0-私密
     * @param status
     * @return
     */
    public String parseTransactionStatus2(String status)
    {
        switch (status)
        {
            case "公开":
                return "1";
            case "私密":
                return "0";
            default:
                return null;
        }
    }

    /**
     * 解析事务结论:0-待定位;1-需求；2-问题
     * @param conclusion
     * @return
     */
    public Integer parseTransactionConclusion(String conclusion)
    {
        switch (conclusion)
        {
            case "待定位":
                return 0;
            case "需求":
                return 1;
            case "问题":
                return 2;
            default:
                return null;
        }
    }

    /**
     * 解析事务是否紧急:1-紧急;0不紧急,默认不紧急
     * @return
     */
    public Integer parseIsUrgency(String isUrgencyStr)
    {
        switch (isUrgencyStr)
        {
            case "紧急":
                return 1;
            default:
                return 0;
        }
    }
}
